﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Reflection;

namespace X10HomeServer
{
    class X10Server : HttpServer
    {
        private string _html = string.Empty;

        public X10Server(IPAddress interfaceIP, int port, int timeout = 5000)
            : base(interfaceIP, port, timeout)
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("X10HomeServer.index.html"))
            {
                if (stream != null)
                    using (StreamReader streamReader = new StreamReader(stream))
                        _html = streamReader.ReadToEnd();
            }
        }

        /// <summary>
        /// POST request handler
        /// </summary>
        /// <param name="processor"></param>
        /// <param name="inputData"></param>
        public override void HandlePOSTRequest(HttpProcessor processor, StreamReader inputData)
        {
            processor.WriteSuccess("text/html");
            processor.OutputStream.Write(_html);
        }

        /// <summary>
        /// POST requests handler
        /// </summary>
        /// <param name="p"></param>
        public override void HandleGETRequest(HttpProcessor processor)
        {
            processor.WriteSuccess("text/html");
            processor.OutputStream.Write(_html);
        }
    }
}
